package com.dream.demo.common;

import lombok.Getter;

/**
 * 给前端返回的统一的返回值
 */
@Getter
public class ResultCommon<T> {
    //编号 前端根据这个编号来判断 这次请求是否成功200
    private int code;
    //信息
    private String msg;
    //任意数据：集合，对象
    private T  data;

    private ResultCommon(int code,String msg){
        this.code =code;
        this.msg = msg;
    }
    private ResultCommon(ResultEnum resultEnum){
        this.code =resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }
    private ResultCommon(int code,String msg,T data){
        this(code,msg);
        this.data = data;
    }
    //请求是否成功
    public boolean isSuccess(){
        return  this.code==Constant.CODE;
    }

    public static ResultCommon success(){
        return new ResultCommon(ResultEnum.SUCCESS);
    }
    public static ResultCommon success(String msg){
        return new ResultCommon(Constant.CODE,msg);
    }
    //成功携带数据 查询
    public static <T>ResultCommon success(T data){
        ResultCommon success = success();
        success.data = data;
        return success;
    }
    public static ResultCommon fail(){
        return new ResultCommon(ResultEnum.FAIL);
    }

    public static ResultCommon fail(ResultEnum resultEnum){
        return new ResultCommon(resultEnum);
    }

    public static <T>ResultCommon fail(T data){
        ResultCommon fail = fail();
        fail.data = data;
        return fail;
    }

    public static ResultCommon fail(int code,String msg){
        ResultCommon objectResultCommon = new ResultCommon<>(code, msg);
        return objectResultCommon;
    }

}
